package com.feonix.common.view.permission;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.feonix.common.permission.AbsPermissionAdapter;
import com.feonix.common.permission.PermissionAdapterHander;
import com.feonix.common.permission.PermissionStructure;
import com.feonix.common.util.ReflectionUtils;
import com.feonix.common.util.StringUtil;

public class PermissionTag extends RequestContextAwareTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3885040217494923563L;
	private String rootPath = "/manager";
	private String moduleId;
	private String functionId;
	private String params;
	private String allowedAdmin;
	private String allowedGroup;
	private String adapter;
	private String method;

	protected int doStartTagInternal() throws Exception {
		PermissionStructure permissionStructure = new PermissionStructure();
		permissionStructure.setFunctionId(this.functionId);
		permissionStructure.setModuleId(this.moduleId);
		permissionStructure.setParams(this.params);
		permissionStructure.setRootPath(this.rootPath);
		permissionStructure.setAllowedAdmin(BooleanUtils.toBooleanObject(this.allowedAdmin));
		permissionStructure.setAllowedGroup(BooleanUtils.toBooleanObject(this.allowedGroup));
		AbsPermissionAdapter absPermissionAdapter = PermissionAdapterHander.getAdapterWithDefault(this.adapter);
		boolean bool = false;
		if (absPermissionAdapter != null) {
			if (StringUtil.isNotEmpty(this.method))
				bool = ((Boolean) ReflectionUtils.invokeMethod(absPermissionAdapter, this.method, null, null))
						.booleanValue();
			else {
				bool = absPermissionAdapter.checkTag(permissionStructure);
			}
		}
		if (bool) {
			return 1;
		}
		return 0;
	}

	public String getModuleId() {
		return this.moduleId;
	}

	public void setModuleId(String moduleId) {
		this.moduleId = moduleId;
	}

	public String getFunctionId() {
		return this.functionId;
	}

	public void setFunctionId(String functionId) {
		this.functionId = functionId;
	}

	public String getAllowedAdmin() {
		return this.allowedAdmin;
	}

	public void setAllowedAdmin(String allowedAdmin) {
		this.allowedAdmin = allowedAdmin;
	}

	public String getAllowedGroup() {
		return this.allowedGroup;
	}

	public void setAllowedGroup(String allowedGroup) {
		this.allowedGroup = allowedGroup;
	}

	public String getRootPath() {
		return this.rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public String getParams() {
		return this.params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getAdapter() {
		return this.adapter;
	}

	public void setAdapter(String adapter) {
		this.adapter = adapter;
	}

	public String getMethod() {
		return this.method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

}
